package com.sheep.classroompie.controller;


import com.sheep.classroompie.entity.*;
import com.sheep.classroompie.service.CourseService;
import com.sheep.classroompie.util.GenerateCourseId;
import com.sheep.classroompie.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin("http://localhost:8080")
@RequestMapping("/course")
public class CourseController {
  @Autowired
  CourseService courseService;
  @RequestMapping("/selectAll")
  @ResponseBody
  public List<Course> querySelectedCourse(@RequestBody StudentIdRequest request){
    Integer stuId = request.getStuId();
    System.out.println(stuId);
    return courseService.querySelectedCourse(stuId);
  }

  @RequestMapping("/selectTaughtCourse")
  @ResponseBody
  public List<Course> queryTaughtCourse(@RequestBody TeacherIdRequest teacherIdRequest){
    Integer teacherId = teacherIdRequest.getTeacherId();
    System.out.println(teacherId);
    return courseService.queryTaughtCourse(teacherId);
  }
  @RequestMapping("/selectById")
  @ResponseBody
  public Course queryCourseById(@RequestBody CourseIdRequest courseIdRequest){
    String courseId = courseIdRequest.getCourseId();
    System.out.println("课程码:" +courseId);
    return courseService.selectCourseById(courseId);
  }

  @RequestMapping("/addCourse")
  @ResponseBody
  public Boolean addCourse(@RequestBody Course course){
    String courseId = GenerateCourseId.generateCourseId();
    Course newCourse = new Course(courseId,0,course.getCourseName(),course.getClassName(),
                                  course.getTeacherId(),course.getTerm(),course.getTeacherName());
    return courseService.addCourse(newCourse);
  }

  @RequestMapping("/deleteCourse")
  @ResponseBody
  public boolean deleteCourse(@RequestBody CourseIdRequest courseIdRequest){
    String courseId = courseIdRequest.getCourseId();
    return courseService.deleteCourse(courseId);
  }

  @RequestMapping("/joinCourse")
  @ResponseBody
  public boolean joinCourse(@RequestBody SelectedCourse selectedCourse){
    Integer stuId = selectedCourse.getStuId();
    String courseId = selectedCourse.getCourseId();
    System.out.println("学号:"+stuId + "课程码:" +courseId);
    if(courseService.joinCourse(stuId,courseId)){
      courseService.updateCourseMembersCount(courseId);
      return true;
    }else{
      return false;
    }
  }

  @RequestMapping("/selectStudentIdByCourseId")
  @ResponseBody
  public List<String> selectStudentIdByCourseId(@RequestBody Course course){
    return courseService.selectStudentIdByCourseId(course.getCourseId());
  }
}
